package smedi.ismedi.web.bff.controller.safetyeducation;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.safety.education.api.dto.request.PreFestivalSafetyEducationQueryRequest;
import com.smedi.ismedi.safety.education.api.dto.request.PreFestivalSafetyEducationUpdateRequest;
import com.smedi.ismedi.safety.education.api.dto.response.PaginationResponse;
import com.smedi.ismedi.safety.education.api.dto.response.PreFestivalSafetyEducationResponse;
import com.smedi.ismedi.safety.education.api.feign.RemotePreFestivalSafetyEducationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webbff/v1/preFestivalSafetyEducation")
@RequiredArgsConstructor
@Api(tags = "节前教育API")
public class PreFestivalSafetyEducationController extends BaseController {
    private final RemotePreFestivalSafetyEducationFeignApi remotePreFestivalSafetyEducationFeignApi;
    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    /**
     * 查询节前教育
     *
     * @param preFestivalSafetyEducationQueryRequest
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询节前教育")
    @GetMapping(value = "/queryPreFestivalSafetyEducation")
    public ResponseData<PaginationResponse> getQueryAllPreFestivalSafetyEducationFeign(@Validated @SpringQueryMap PreFestivalSafetyEducationQueryRequest preFestivalSafetyEducationQueryRequest) {
        return remotePreFestivalSafetyEducationFeignApi.getQueryAllPreFestivalSafetyEducationFeign(preFestivalSafetyEducationQueryRequest);
    }

    /**
     * 创建节前教育
     *
     * @param preFestivalSafetyEducationUpdateRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "创建节前教育")
    @PostMapping(value = "/savePreFestivalSafetyEducation")
    public ResponseData<PreFestivalSafetyEducationResponse> postSavePreFestivalSafetyEducation(@Validated @RequestBody PreFestivalSafetyEducationUpdateRequest preFestivalSafetyEducationUpdateRequest) {
        return remotePreFestivalSafetyEducationFeignApi.postSavePreFestivalSafetyEducation(preFestivalSafetyEducationUpdateRequest);
    }

    /**
     * 删除节前教育
     *
     * @param ids
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除节前教育")
    @DeleteMapping(value = "/deletePreFestivalSafetyEducation")
    public ResponseData deletePreFestivalSafetyEducation(@Validated @RequestBody List<Long> ids) {
        return remotePreFestivalSafetyEducationFeignApi.deletePreFestivalSafetyEducation(ids);
    }

    /**
     * 查询单个节前教育信息
     *
     * @param id
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "查询单个节前教育信息")
    @GetMapping(value = "/queryPreFestivalSafetyEducationInfo")
    public ResponseData<PreFestivalSafetyEducationResponse> getPreFestivalSafetyEducationInfo(@RequestParam("id") Long id) {
        return remotePreFestivalSafetyEducationFeignApi.getPreFestivalSafetyEducationInfo(id);
    }

    /**
     * 预览单个节前教育信息
     *
     * @param id
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "GET", value = "预览单个节前教育信息")
    @GetMapping(value = "/previewPreFestivalSafetyEducationInfo")
    public ResponseData<String> previewPreFestivalSafetyEducation(@RequestParam("id") Long id, HttpServletResponse response) throws IOException {
        String signKey = remotePreFestivalSafetyEducationFeignApi.exportPreFestivalSafetyEducation(id).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    /**
     * 导出单个节前教育信息
     *
     * @param id
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "GET", value = "导出单个节前教育信息")
    @GetMapping(value = "/exportPreFestivalSafetyEducationInfo")
    public ResponseData<Boolean> exportPreFestivalSafetyEducation(@RequestParam("id") Long id, HttpServletResponse response) throws IOException {
        String signKey = remotePreFestivalSafetyEducationFeignApi.exportPreFestivalSafetyEducation(id).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }

    /**
     * 导出多个节前教育信息
     *
     * @param ids
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "POST", value = "预览多个节前教育信息")
    @PostMapping(value = "/previewPreFestivalSafetyEducationBatch")
    public ResponseData<String> previewPreFestivalSafetyEducationBatch(@Validated @RequestBody List<Long> ids, HttpServletResponse response) {
        String signKey = remotePreFestivalSafetyEducationFeignApi.exportPreFestivalSafetyEducationBatch(ids).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        String url = FileHelper.getUrl(FDFS_SERVICE_ADDRESS_OUT, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(url);
    }

    /**
     * 导出多个节前教育信息
     *
     * @param ids
     * @return
     * @throws IOException
     */
    @ApiOperation(httpMethod = "POST", value = "导出多个节前教育信息")
    @PostMapping(value = "/exportPreFestivalSafetyEducationBatch")
    public ResponseData<Boolean> exportPreFestivalSafetyEducationBatch(@Validated @RequestBody List<Long> ids, HttpServletResponse response) {
        String signKey = remotePreFestivalSafetyEducationFeignApi.exportPreFestivalSafetyEducationBatch(ids).getData();
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);

        return new ResponseData<>().success(true);
    }
}
